package com.wanglibao.woodtrade.woodtrade.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wanglibao.woodtrade.woodtrade.R;
import com.wanglibao.woodtrade.woodtrade.config.NetConfig;
import com.wanglibao.woodtrade.woodtrade.jsonrpc.JsonRPCAsyncTask;
import com.wanglibao.woodtrade.woodtrade.ui.fragment.adapter.HomeFragmentRecommendAdapter;
import com.wanglibao.woodtrade.woodtrade.ui.fragment.adapter.model.Commodity;
import com.wanglibao.woodtrade.woodtrade.util.PullUtil;
import com.wanglibao.woodtrade.woodtrade.util.ToastUtil;
import com.wanglibao.woodtrade.woodtrade.widget.PullToRefreshView.PullToRefreshBase;
import com.wanglibao.woodtrade.woodtrade.widget.PullToRefreshView.PullToRefreshListView;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SearchActivity extends Activity implements AdapterView.OnItemClickListener, PullToRefreshBase.OnRefreshListener2 {

    @Bind(R.id.ll_activity_search_title)
    LinearLayout llActivitySearchTitle;
    @Bind(R.id.edit_activity_search_search)
    EditText editActivitySearchSearch;

    @Bind(R.id.list_activity_search_listdata)
    PullToRefreshListView listActivitySearchListdata;

    @Bind(R.id.ll_activity_search_null)
    LinearLayout llActivitySearchNull;

    Commodity commodity;

    int pageIndex = 1;

    HomeFragmentRecommendAdapter homeFragmentRecommendAdapter;

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {

            if (msg.arg2 == 2) {
                if (pageIndex == 1) {
                    if (commodity != null && commodity.getList() != null) {
                        commodity.getList().clear();
                    }
                    homeFragmentRecommendAdapter.update(commodity);
                    llActivitySearchNull.setVisibility(View.VISIBLE);
                    listActivitySearchListdata.setVisibility(View.GONE);
                } else {
                    ToastUtil.createLongToast(SearchActivity.this, "没有更多数据了");
                    pageIndex--;
                }

            } else {
                Commodity commodity1 = new Gson().fromJson(msg.obj.toString(), Commodity.class);
                if (pageIndex == 1) {
                    commodity = commodity1;
                } else {
                    commodity.getList().addAll(commodity1.getList());
                }
                homeFragmentRecommendAdapter.update(commodity);
                llActivitySearchNull.setVisibility(View.GONE);
                listActivitySearchListdata.setVisibility(View.VISIBLE);
            }
            listActivitySearchListdata.onRefreshComplete();
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        ButterKnife.bind(this);
        if (Build.VERSION.SDK_INT >= 19) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        } else {
            llActivitySearchTitle.setPadding(0, 0, 0, 0);
        }
        initView();
    }

    private void initView() {

        commodity = new Commodity();
        commodity.setList(new ArrayList<Commodity.ListBean>());
        homeFragmentRecommendAdapter = new HomeFragmentRecommendAdapter(this, commodity);
        listActivitySearchListdata.getRefreshableView().setAdapter(homeFragmentRecommendAdapter);
        listActivitySearchListdata.getRefreshableView().setOnItemClickListener(this);
        listActivitySearchListdata.setMode(PullToRefreshBase.Mode.BOTH);
        listActivitySearchListdata.getRefreshableView().setSelector(new ColorDrawable(Color.TRANSPARENT));
        listActivitySearchListdata.setOnRefreshListener(this);

        PullUtil.initIndicator(listActivitySearchListdata);

        editActivitySearchSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if(actionId == EditorInfo.IME_ACTION_SEARCH){
                    onSearchClick();
                }
                return false;
            }
        });

        loadData("");
    }

    @OnClick(R.id.imgbtn_activity_search_back)
    void onImgbtnActivitySearchBackClick() {
        onBackPressed();
    }

    @OnClick(R.id.tv_layout_null_sentInfo)
    void onSentInfoClick() {
        startActivity(new Intent(this, SupplyAndDemendActivity.class));
        finish();
    }

    @OnClick(R.id.tv_activity_search_search)
    void onSearchClick() {
        pageIndex = 1;
        loadData(editActivitySearchSearch.getText().toString());
    }
    //SERVER_wangxiaoming
    public void loadData(String keyword) {
        JsonRPCAsyncTask jsonRPCAsyncTask = new JsonRPCAsyncTask(false,this, NetConfig.SERVER + NetConfig.TYPE_GOODS,
                NetConfig.lists, handler, "jsonObject", NetConfig.TaskId.lists,
                new Object[]{pageIndex, keyword, "", "", ""});
        jsonRPCAsyncTask.execute(new Void[]{});
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(this, CommodityActivity.class);
        intent.putExtra("id",commodity.getList().get(position-1).getGoods_id());
        startActivity(intent);
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        pageIndex = 1;
        loadData(editActivitySearchSearch.getText().toString());
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        pageIndex++;
        loadData(editActivitySearchSearch.getText().toString());
    }
}
